package com.znsd.utils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import sun.misc.BASE64Encoder;

public class FormatUtil {

	public static String getImageStr(String pk) {
		if (pk.equals("") && pk.length() == 0) {
			pk = "../webapp/img/1.jepg";
		}
		String imgFile = pk;
		InputStream in = null;
		byte[] data = null;
		try {
			in = new FileInputStream(imgFile);
			data = new byte[in.available()];
			in.read(data);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		BASE64Encoder encoder = new BASE64Encoder();
		if (data == null) {
			return "";
		} else {
			return encoder.encode(data);
		}
	}

	// 格式化题目
	public static String sub(String name, int size) {
		if (name != null) {
			int length = name.length();
			String s1 = "";
			String s2 = "<br/>";
			if (length > size) {
				for (int i = 0; i < name.length(); i++) {
					if (i > 0 && i % size == 0) {// 7 14 21
						if (i == size) {
							s1 += name.substring(0, i) + s2;
							if (i + size <= name.length()) {
								if (i + size + 1 <= name.length()) {
									s1 += name.substring(i, i + size) + s2;
								} else {
									s1 += name.substring(i, i + size);
								}
							} else {
								s1 += name.substring(i);
							}
						} else {
							if (i + size > name.length()) {
								s1 += name.substring(i);
							} else {
								if (i + size + 1 <= name.length()) {
									s1 += name.substring(i, i + size) + s2;
								} else {
									s1 += name.substring(i);
								}
							}
						}
					}
				}
				name = s1;
				/*
				 * String s1 = name.substring(0,size)+"<br/>"; String s2 =
				 * name.substring(size,length); name = s1+s2;
				 */
			}
		}
		return name;
	}

}
